<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <img alt="logo" class="logo" src="@/assets/img/logo.png" />
        <span class="title">{{ systemName }}</span>
      </div>
      <div class="desc">Win Admin created by fyl</div>
    </div>
    <div class="login">
      <a-form-model ref="form" :model="form" :rules="rules" @submit="onSubmit">
        <a-form-model-item prop="name">
          <a-input v-model="form.name" size="large" placeholder="admin" autocomplete="off"><a-icon slot="prefix" type="user" /></a-input>
        </a-form-model-item>
        <a-form-model-item prop="password">
          <a-input-password v-model="form.password" size="large" autocomplete="off"><a-icon slot="prefix" type="lock" /></a-input-password>
        </a-form-model-item>
        <a-form-model-item prop="code">
          <a-input v-model="form.code" size="large" style="width: 200px; margin: 0 8px 8px 0" autocomplete="off"><a-icon slot="prefix" type="code" /></a-input>
          <img style="float: right" :src="codeUrl" @click="getCode" />
        </a-form-model-item>
        <div>
          <a-checkbox v-model="form.rememberMe">自动登录</a-checkbox>
          <a style="float: right">忘记密码</a>
        </div>

        <a-form-model-item>
          <a-button :loading="logging" style="width: 100%; margin-top: 24px" size="large" htmlType="submit" type="primary">登录</a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
  </common-layout>
</template>

<script>
import Cookies from 'js-cookie'

import { mapState } from 'vuex'
import CommonLayout from '@/layouts/CommonLayout'

import { code } from '@/api/login'

export default {
  name: 'Login',
  components: { CommonLayout },
  data() {
    return {
      codeUrl: null,
      form: {
        formId: '',
        name: '',
        password: '',
        code: '',
        rememberMe: false
      },
      rules: {
        name: [{ required: true, message: '请输入账户名', whitespace: true, trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', whitespace: true, trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', whitespace: true, trigger: 'blur' }]
      },
      logging: false
    }
  },
  created() {
    // 获取验证码
    this.getCode()
    // 获取用户名密码等Cookie
    this.getCookie()
  },
  computed: {
    ...mapState('setting', ['systemName', 'footerLinks', 'copyright'])
  },
  methods: {
    async getCode() {
      const res = await code()
      this.form.formId = res.data.uuid
      this.codeUrl = res.data.img
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')

      this.form = {
        formId: '',
        name: username === undefined ? '' : username,
        password: password === undefined ? '' : password,
        code: '',
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    onSubmit(e) {
      e.preventDefault()
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        this.logging = true
        const user = { ...this.form }
        if (user.rememberMe) {
          Cookies.set('username', user.name, { expires: 1 })
          Cookies.set('password', user.password, { expires: 1 })
          Cookies.set('rememberMe', user.rememberMe, { expires: 1 })
        } else {
          Cookies.remove('username')
          Cookies.remove('password')
          Cookies.remove('rememberMe')
        }

        this.$store
          .dispatch('user/login', user)
          .then(() => this.$router.push({ path: this.$route.query.redirect || '/' }))
          .catch((error) => console.log(error))
          .finally(() => (this.logging = false))
      })
    }
  }
}
</script>

<style lang="less" scoped>
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 33px;
        color: @title-color;
        font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 368px;
    margin: 0 auto;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
